package com.lyw.forum.controller;

import com.lyw.forum.common.enums.ResultCode;
import com.lyw.forum.pojo.request.MessageCreateRequest;
import com.lyw.forum.pojo.response.AppResult;
import com.lyw.forum.pojo.response.MessageSelectResponse;
import com.lyw.forum.pojo.response.UserSelectResponse;
import com.lyw.forum.service.IMessageService;
import com.lyw.forum.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;
    @RequestMapping("/send")
    public AppResult create(@RequestBody @Validated MessageCreateRequest messageCreateRequest, HttpServletRequest request) {
        //获取用户id
        String id = request.getHeader("id");

        //获取当前登录用户的信息
        UserSelectResponse userSelectResponse = userService.selectById(Integer.parseInt(id));

        //判断用户是否被禁言
        if (userSelectResponse.getState() == 1) {
            //用户已禁言 打印日志
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            //但会结果
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        //不能给自己发站内信
        if (Integer.parseInt(id) == messageCreateRequest.getReceiveUserId()) {
            return AppResult.fail("不能给自己发站内信");
        }

        //校验接收者是否存在
        UserSelectResponse receiveUser = userService.selectById(messageCreateRequest.getReceiveUserId());

        if (receiveUser == null || receiveUser.getDeleteState() == 1) {
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        //设置发送者id
        messageCreateRequest.setPostUserId(Integer.parseInt(id));

        //调用业务逻辑
        messageService.create(messageCreateRequest);

        //返回结果
        return AppResult.success("发送成功");
    }

    @RequestMapping("/selectUnreadCount")
    public AppResult selectUnreadCount(HttpServletRequest request) {
        //获取用户id
        String id = request.getHeader("id");

        //调用业务逻辑
        Integer integer = messageService.selectUnreadCount(Integer.parseInt(id));//当前用户就是接收者的用户

        //返回结果
        return AppResult.success(integer);
    }

    @RequestMapping("/getAll")
    public AppResult selectByReceiveUserId(HttpServletRequest request) {
        //获取用户id
        String id = request.getHeader("id");

        //获取当前登录用户信息
        UserSelectResponse userSelectResponse = userService.selectById(Integer.parseInt(id));

        if (userSelectResponse == null) {
            //用户不存在
            log.error(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        //调用业务逻辑 并返回结果
        return AppResult.success(messageService.selectByReceiveUserId(Integer.parseInt(id)));
    }

    @RequestMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,@NotNull(message = "站内信id不能为空") Integer id) {
        //根据id查询站内信
        MessageSelectResponse messageSelectResponse = messageService.selectById(id);

        //判断站内信是否存在
        if (messageSelectResponse == null || messageSelectResponse.getDeleteState() == 1) {
            //站内信不存在
            log.error(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_NOT_EXISTS);
        }

        //判断站内信是不是自己的
        if (messageSelectResponse.getReceiveUserId() != Integer.parseInt(request.getHeader("id"))) {
            //返回错误信息
            return AppResult.fail(ResultCode.FAILED_FORBIDDEN);
        }

        //调用业务逻辑
        messageService.updateStateById(id, (byte) 1);

        //返回结果
        return AppResult.success();
    }

    @RequestMapping("/reply")
    public AppResult reply(HttpServletRequest request, Integer repliedId, String content) {
        //获取用户id
        String id = request.getHeader("id");

        //校验当前用户状态
        UserSelectResponse userSelectResponse = userService.selectById(Integer.parseInt(id));

        if (userSelectResponse.getState() == 1) {
            //用户已禁言 打印日志
            log.error(ResultCode.FAILED_USER_BANNED.toString());
            //返回结果
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }

        //校验被回复的站内信是否存在
        MessageSelectResponse messageSelectResponse = messageService.selectById(repliedId);

        if (messageSelectResponse == null || messageSelectResponse.getDeleteState() == 1) {
            //被回复的站内信不存在
            return AppResult.fail(ResultCode.FAILED_NOT_EXISTS);
        }

        //构建请求体
        MessageCreateRequest messageCreateRequest = new MessageCreateRequest();
        messageCreateRequest.setPostUserId(Integer.parseInt(id));//发送者id
        messageCreateRequest.setReceiveUserId(messageSelectResponse.getPostUserId()); //接收者id
        messageCreateRequest.setContent(content);

        //调用service
        messageService.reply(repliedId, messageCreateRequest);

        //返回结果
        return AppResult.success();
    }
}
